//这种方式缺点无法查找重复元素的左侧边界，也就是说得到的索引值不是最边侧的
function binarySearch(nums, target) {
    var left = 0;
    var right = nums.length - 1; // 注意

    while (left <= right) {
        var mid = Math.floor(left + (right - left) / 2);
        if (nums[mid] == target)
            return mid;
        else if (nums[mid] < target)
            left = mid + 1; // 注意
        else if (nums[mid] > target)
            right = mid - 1; // 注意
    }
    return -1;
}

var arr = [1, 2, 2, 2, 3];
console.log(binarySearch(arr, 2)); 